package com.example.spzs.controller;

import com.example.spzs.Service.getgoodsService;
import com.example.spzs.mapper.goodsMapper;
import com.example.spzs.pojo.goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;


@Controller
public class showController {
    @Autowired
    private goodsMapper goodsMapper;
    @Autowired
    private getgoodsService getgoodsService;
    List<goods>goodsList=null;
    /**
     *  初始路径
     * *
     */
    @RequestMapping("/")
    public String show(Model model){
        //先查询
        goodsList = goodsMapper.queryAll();
        //向前端设置值
        model.addAttribute("goods",new goods());
        model.addAttribute("msg",goodsList);
        return  "show";

    }
    /**
     *  查询
     * *
     */
    @PostMapping("/qur")
    public String query(Model model, @ModelAttribute goods goods){
        String goodsName=goods.getName();
        if (goodsName!=null&&!goodsName.equals("")){
            goodsList= goodsMapper.queryByLikeName("%"+goodsName+"%");}
        model.addAttribute("msg",goodsList);
        return "show";
    }
    /**
     *  删除
     * *
     */
    @RequestMapping(path = "/del")
    public String delete(Model model, @RequestParam(name = "name") String goodsName){
        if (goodsName!=null&&!goodsName.equals("")){
             goodsMapper.deleteGoods(goodsName);
        }
        goodsList = goodsMapper.queryAll();
        model.addAttribute("msg",goodsList);
        return "redirect:/";
   }
    /**
     *  加载
     * *
     */
   @PostMapping(path = "/load")
   public String load(){
       try {
           getgoodsService.execute();
       }catch (Exception e){
           e.printStackTrace();
       }
       return "redirect:/";
   }

}
